/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import ProgramUtils.SQLManager;
import ProgramUtils.Usertask;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author TeslaSolari
 */
public class Register {

    public void runGUI() throws SQLException {
        RegisterC frame = new RegisterC();
        frame.setSize(300, 350);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
    public void hideGUI()
    {
    
    }
}

class RegisterC extends JFrame implements ActionListener {

    //Create fields
    JLabel usernameL, fNameL, sNameL, emailL, passL, groupL;
    JTextField usernameT, fnameT, sNameT, emailT, passT;
    JPanel panel;
    JButton register = new JButton("Register");
    JButton back = new JButton("back");
    int count = 0;
    ResultSet rs = SQLManager.executeQuery("SELECT * FROM groups");
    String[] accountTypes;
    JComboBox accountTypesC;

    RegisterC() throws SQLException {
        //setup text fields
        usernameT = new JTextField(15);
        fnameT = new JTextField(15);
        sNameT = new JTextField(15);
        emailT = new JTextField(15);
        passT = new JPasswordField(15);

        //get data for combo box
        rs.last();
        accountTypes = new String[rs.getRow()];
        rs.first();
        System.out.println(rs.getString(2));

        do {
            accountTypes[count] = rs.getString(2);
            count++;
        } while (rs.next());
        accountTypesC = new JComboBox(accountTypes);

        //set text for labels
        usernameL = new JLabel();
        usernameL.setText("Username");
        fNameL = new JLabel();
        fNameL.setText("Name");
        sNameL = new JLabel();
        sNameL.setText("Surname");
        emailL = new JLabel();
        emailL.setText("email");
        passL = new JLabel();
        passL.setText("Password");
        groupL = new JLabel();
        groupL.setText("UserGroup");

        //set text for buttons
        back = new JButton("back");
        register = new JButton("register");

        //config panel
        panel = new JPanel(new GridLayout(7, 1));
        panel.add(usernameL);
        panel.add(usernameT);
        panel.add(fNameL);
        panel.add(fnameT);
        panel.add(sNameL);
        panel.add(sNameT);
        panel.add(emailL);
        panel.add(emailT);
        panel.add(passL);
        panel.add(passT);
        panel.add(groupL);
        panel.add(accountTypesC);
        panel.add(back);
        panel.add(register);
        add(panel, BorderLayout.CENTER);
        back.addActionListener(this);
        register.addActionListener(this);
        setTitle("REGISTRATION FORM");

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            String btp = e.getActionCommand();
            switch (btp) {
                case "register":
                    Usertask obj = new Usertask();
                    boolean proceed = false;
                    if (usernameT.getText().equals("")) {
                        usernameT.setText(JOptionPane.showInputDialog("please insert a username"));
                    } else if (obj.userExist(usernameT.getText())) {
                        usernameT.setText(JOptionPane.showInputDialog("please insert a new username"));
                    } else if (obj.emailExist(emailT.getText())) {
                        emailT.setText(JOptionPane.showInputDialog("Email Exists please insert a new email adrease"));
                    } else if (emailT.getText().equals("")) {
                        emailT.setText(JOptionPane.showInputDialog("No email adrease entered please enter an email adrease"));
                    } else if (passT.getText().equals("")) {
                        JOptionPane.showMessageDialog(rootPane, "no password entered please insert password");
                    } else if (accountTypesC.getSelectedItem().equals("admin") & obj.isLogedIn() == false & obj.currentUserIsAdmin() == false) {
                        JOptionPane.showMessageDialog(rootPane, "Admin Acounts can only be made once loged in with admin account");
                    } else {
                        SQLManager.insertQuery("INSERT INTO users(username, fname, sname, email, pass, type) "
                                + "VALUES('" + usernameT.getText() + "', '" + fnameT.getText() + "', '" + sNameT.getText() + "', '" + emailT.getText() + "', '" + passT.getText() + "', '" + accountTypesC.getSelectedItem() + "')");
                    }
                    break;
                case "back":
                    break;
            }
        } catch (SQLException ex) {
            Logger.getLogger(RegisterC.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
